import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';

import { fetchValidation } from '@/utils';

import {
    queryDeliveryList,
    queryCount,
    querySchoolList,
    cancelUnmanned
} from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        fetchSchoolList: Effect;
        cancel: Effect;
        count: Effect;
    };
    reducers: {
        save: Reducer;
        cleanCount: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'logisCenterUnmannedList',

    state: {
        data: {
            content: [],
            schoolList: [],
            count: 0,
        },
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const { current, } = payload.params;
            const response = yield call(queryDeliveryList, payload.params);
            const data = fetchValidation(response);
            yield data && put({
                type: 'save',
                payload: {
                    type: 'list',
                    value: {
                        current: current,
                        // search: payload.search,
                        list: data.body ? data.body.list : [],
                    },
                },
            });
        },
        *fetchSchoolList({ payload }, { call, put }) {
            // 获取校园小区列表
            const response = yield call(querySchoolList, payload);
            const data = fetchValidation(response);
            if (data) {
                yield data &&
                    put({
                        type: 'save',
                        payload: { type: 'school', value: data.body },
                    });
            }
        },
        * count({ payload }, { call, put }) {
            const { pageIndex, pageSize, ...query } = payload;
            const response = yield call(queryCount, query);
            const data = fetchValidation(response);
            if (data) {
                yield put({
                    type: 'save',
                    payload: { type: 'count', value: data.body },
                });
            }
        },
        * cancel({ payload }, { call, put }) {
            const response = yield call(cancelUnmanned, payload.params);
            const data = fetchValidation(response);
            if (data) {
                message.success(data.msg);
                yield put({
                    type: 'fetch',
                    payload: { params: payload.query },
                });
            }
        },
    },

    reducers: {
        save({ data }, { payload }) {
            if (payload.type === 'school') {
                data.schoolList = payload.value;
            } else if (payload.type === 'list') {
                const pos = data.content.findIndex((item: any) => item.current === payload.value.current);
                if (pos === -1) {
                    data.content.push(payload.value);
                } else {
                    data.content[pos] = payload.value;
                }
            } else if (payload.type === 'count') {
                data.count = payload.value;
            }
            return { data };
        },
        cleanCount({ data }) {
            data.count = 0;
            return { data };
        },
    },
};

export default Model;
